package com.server.api.controller;

import com.server.api.common.BaseController;
import com.server.api.common.ReturnMsg;
import com.server.api.dto.${className}DTO;
import com.server.api.service.${className}Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
<#if selectEntities??>
import com.server.api.common.BaseSelectEntity;
import cn.hutool.core.collection.CollectionUtil;
    <#list selectEntities as selectCol>
import com.server.api.dto.${selectCol['className']}DTO;
import com.server.api.service.${selectCol['className']}Service;
    </#list>
</#if>

/**
* @Description: ${tableComment}对外接口层
* @Author: wuming
*/
@RestController
@RequestMapping("/${unClassName}")
public class ${className}Controller extends BaseController<${className}Service, ${className}DTO> {

    @Override
    @Resource(name = "${unClassName}Service")
    protected void setBaseService(${className}Service ${unClassName}Service) {
        this.baseService = ${unClassName}Service;
    }
    <#if selectEntities??>
        <#list selectEntities as selectCol>
    @Autowired
    private ${selectCol['className']}Service ${selectCol['unClassName']}Service;
        </#list>
    </#if>
    <#if hasUpload>
    /**
    * 图片上传文件夹名称
    */
    private final String realPath = "/${unClassName}";

    /**
    * 图片上传
    *
    * @param filename
    * @param id 需要修改的数据主键id
    * @return
    * @throws IOException
    */
    @PostMapping("/upload/{id}")
    public ReturnMsg upload(@RequestParam("file") MultipartFile filename, @PathVariable("id") Long id) throws IOException {
        return this.baseUpLoad(filename, id, realPath, ${className}DTO.class) ? ReturnMsg.ok() : ReturnMsg.error();
    }
    </#if>

    /**
    * 默认分页查询
    * @param ${unClassName}DTO
    * @return
    * @throws IOException
    */
    @PostMapping("/page")
    public ReturnMsg page(@RequestBody ${className}DTO ${unClassName}DTO) {
        return ReturnMsg.ok(baseService.basePage(${unClassName}DTO));
    }


<#if selectEntities??>
    <#list selectEntities as selectCol>
    @GetMapping("/get${selectCol['className']}Data")
    public ReturnMsg get${selectCol['className']}Data(){
        List<${selectCol['className']}DTO> dataList = ${selectCol['unClassName']}Service.list();
        List<BaseSelectEntity> selectEntities = new ArrayList<>();
            if(CollectionUtil.isNotEmpty(dataList)){
            dataList.forEach(item -> {
                BaseSelectEntity baseSelectEntity = new BaseSelectEntity();
                baseSelectEntity.setKey(item.get${selectCol['valueCol']}());
                baseSelectEntity.setValue(item.get${selectCol['nameCol']}());
                selectEntities.add(baseSelectEntity);
            });
        }
        return ReturnMsg.ok(selectEntities);
   }
    </#list>
</#if>
}

